package com.sunwayworld.basemodule.business.folder.service.impl;

import com.sunwayworld.framework.data.page.Page;
import com.sunwayworld.framework.database.sql.Order;
import com.sunwayworld.framework.mybatis.mapper.MapperParameter;
import com.sunwayworld.framework.mybatis.mapper.SearchFilter;
import com.sunwayworld.framework.mybatis.page.PageRowBounds;
import com.sunwayworld.framework.support.PersistableHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.sunwayworld.framework.at.annotation.AuditTrailType;
import com.sunwayworld.framework.at.annotation.AuditTrailEntry;
import com.sunwayworld.framework.spring.annotation.GikamBean;

import com.sunwayworld.basemodule.business.folder.dao.LimsPreOrderTaskDao;
import com.sunwayworld.basemodule.business.folder.bean.LimsPreOrderTaskBean;
import com.sunwayworld.basemodule.business.folder.service.LimsPreOrderTaskService;
import com.sunwayworld.framework.context.ApplicationContextHelper;
import com.sunwayworld.framework.restful.data.RestJsonWrapperBean;

import java.util.List;
import java.util.stream.Collectors;

@Repository
@GikamBean
public class LimsPreOrderTaskServiceImpl implements LimsPreOrderTaskService {

    @Autowired
    private LimsPreOrderTaskDao limsPreOrderTaskDao;

    @Override
    @SuppressWarnings("unchecked")
    public LimsPreOrderTaskDao getDao() {
        return limsPreOrderTaskDao;
    }

    @Override
    @Transactional
    @AuditTrailEntry(AuditTrailType.INSERT)
    public Long insert(RestJsonWrapperBean jsonWrapper) {
        LimsPreOrderTaskBean limsPreOrderTask = jsonWrapper.parseUnique(LimsPreOrderTaskBean.class);
        limsPreOrderTask.setId(ApplicationContextHelper.getNextIdentity());
        getDao().insert(limsPreOrderTask);
        return limsPreOrderTask.getId();
    }

    @Override
    public Page<LimsPreOrderTaskBean> selectBySamplePreTasks(RestJsonWrapperBean wrapperBean) {
        MapperParameter parameter = wrapperBean.extractMapFilter();
        PageRowBounds pageRowBounds = wrapperBean.extractPageRowBounds();
        return this.selectPagination(() -> getDao().selectBySamplePreTasks(parameter), pageRowBounds);
    }

    @Override
    public List<LimsPreOrderTaskBean> selectBySamplePreTasks(SearchFilter filter, Order... orders) {
        MapperParameter parameter = new MapperParameter();
        parameter.setFilter(filter);
        int var5 = orders.length;
        for (int var6 = 0; var6 < var5; ++var6) {
            Order order = orders[var6];
            parameter.setOrderParam(order.getColumn(), order.getDirection());
        }
        return this.selectBySamplePreTasks(parameter);
    }

    @Override
    public List<LimsPreOrderTaskBean> selectBySamplePreTasks(MapperParameter parameter) {
        return getDao().selectBySamplePreTasks(parameter).parallelStream().map(m -> PersistableHelper.mapToPersistable(m, LimsPreOrderTaskBean.class)).collect(Collectors.toList());
    }
}
